﻿#ifndef SERIALPORTMODBUS_H
#define SERIALPORTMODBUS_H

#include <QObject>
#include <QModbusClient>

#include "BaseModbus.h"

class QSerialPortModbus : public BaseModbus
{
    Q_OBJECT
public:
    Q_INVOKABLE explicit QSerialPortModbus(QObject *parent = nullptr);

signals:

public:
    void SetCommunicationParam(QString communicationParam);
    bool Connect();
    bool IsConnected();
    void Dispose();

public slots:
    // ICommunication interface
    void OnError1(QModbusDevice::Error error);

protected:
    QModbusClient *modbusDevice;

    // IModBus interface
public:
    bool ReadOutRegisters(int addr, int count, QList<uint16_t> &retList);
    bool ReadInRegisters(int addr, int count, QList<uint16_t> &retList);
    bool WriteRegisters(int addr, int count, QList<uint16_t> inputList);
    bool ReadOutBits(int addr, int count, QList<bool> &retList);
    bool ReadInBits(int addr, int count, QList<bool> &retList);
    bool WriteBits(int addr, int count, QList<bool> inputList);

private:
    bool ReadRegistersImp(QModbusDataUnit::RegisterType registerType,int addr, int count, QList<uint16_t> &retList);

    // BaseModbus interface
public:
    void CreateModbusInstance() override;
};

#endif // SERIALPORTMODBUS_H
